const PostCompilePlugin = require('webpack-post-compile-plugin')
const TransformModulesPlugin = require('webpack-transform-modules-plugin')
const path = require("path");
// 判断是否是生产环境
let env = process.argv[4];
const isProduction = env === 'buildProd';
console.log(process.argv)
console.log('---------------------')
console.log(process.argv[4]);
//如果不携带参数则默认prod生产环境
if (['dev', 'beta', 'prod'].indexOf(env) < 0) {
  env = 'prod';
}
function resolve(dir) {
    return path.join(__dirname, dir)
}
module.exports = {
    // 基本路径
    publicPath: '/',
    // 输出文件目录
    outputDir: 'dist',
    // eslint-loader 是否在保存的时候检查
    lintOnSave: true,
    // use the full build with in-browser compiler?
    // https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only
    // compiler: false,
    // webpack配置
    // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
    chainWebpack: config => {
        // // 配置别名
        config.resolve.alias
            .set("@", resolve("src"))
            .set("@img", resolve("src/assets/img"))
            .set("@views", resolve("src/views"))
        // 生产环境配置
        config.plugins.delete('preload');
        config.plugins.delete('prefetch');
        if (isProduction) {
            //压缩代码
            config.optimization.minimize(true);
            //分割代码
            // config.optimization.splitChunks({
            //         chunks: 'all'
            //     })
            // 生产环境注入cdn
            // config.plugin('html')
            //     .tap(args => {
            //         args[0].cdn = cdn;
            //         return args;
            //     });
        }
        const conf = config.toConfig()
        config
            .plugin('post-compile')
            .use(PostCompilePlugin, [{
                config: {
                    module: {
                        rules: [...conf.module.rules]
                    }
                }
            }])
            .end()
            .plugin('transform-modules')
            .use(TransformModulesPlugin)
    },
    configureWebpack: config => {
        console.log(3232323,config)
        if (isProduction) {
            //生产环境自动删除console
            config.optimization.minimizer[0].options.terserOptions.compress.warnings = false;
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
            config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true;
            config.optimization.minimizer[0].options.terserOptions.compress.pure_funcs = ['console.log'];
            config.optimization.minimizer[0].options.sourceMap = false;
            config.optimization.minimizer[0].options.parallel = true;
        }
    },
    // vue-loader 配置项
    // https://vue-loader.vuejs.org/en/options.html
    // vueLoader: {},
    // 生产环境是否生成 sourceMap 文件
    productionSourceMap: true,
    // css相关配置
    css: {
        // 是否使用css分离插件 ExtractTextPlugin
        extract: true,
        // 开启 CSS source maps?
        sourceMap: false,
        // css预设器配置项
        loaderOptions: {},
        // 启用 CSS modules for all css / pre-processor files.
        requireModuleExtension : false
    },
    // use thread-loader for babel & TS in production build
    // enabled by default if the machine has more than 1 cores
    parallel: require('os').cpus().length > 1,
    // 是否启用dll
    // See https://github.com/vuejs/vue-cli/blob/dev/docs/cli-service.md#dll-mode
    // webpack-dev-server 相关配置
    devServer: {
        open: process.platform === 'darwin',
        host: '0.0.0.0',
        port: 8080,
        https: false,
        hotOnly: false,
        // proxy: {
        //     '/api': {
        //         //   target: '<url>',
        //         ws: true,
        //         changeOrigin: true
        //     }
        // },
    },
    // 第三方插件配置
    // pluginOptions: {
    // }
}